package com.yxb.cms.handler;

import com.yxb.cms.architect.utils.HttpUtil;
import com.yxb.cms.architect.utils.OpenApiUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 开放接口调用处理
 * @author yangxiaobing
 *
 */
@Component
@Log4j2
public class OpenApiHandler {


    /**
     * 模拟用户查询接口调用，加入签名验证
     * @return
     */
    public String selectUserInfo() throws Exception {
        String url = "http://localhost:8088/api/user_info";
        Map<String,Object> params = new HashMap<>();
        params.put("userAccount","sys002");
        return HttpUtil.post(url,getParamsMap(params));
    }

    /**
     * 组装请求参数信息
     */
    private Map<String,String> getParamsMap(Map<String, Object> requestMap) throws Exception {
        //TODO 分配给接入方的appkey 和 密匙，密匙不随参数传递，安全保存，请勿泄漏。
        String appKey = "BWXE06Xz";
        String appSecret = "ee50f70a00fa12148c9417955508aeb976a9db67";
        String timeStamp = String.valueOf(System.currentTimeMillis());

        String paras = OpenApiUtil.hexParas(requestMap,appSecret);

        String sign = OpenApiUtil.genSign(appKey,timeStamp,paras,appSecret);
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("appKey", appKey);
        paramsMap.put("timeStamp", timeStamp);

        paramsMap.put("paras", paras);
        paramsMap.put("sign", sign);
        return paramsMap;

    }



}
